<?php

namespace Plugins\WebSite\Http\Admin\Controller;


use App\Http\AbstractController;
use App\Middlewares\Admin\AuthenticateMiddleware;
use App\Middlewares\Admin\PermissionsMiddleware;
use App\Util\Response;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\View\Render;
use Plugins\WebSite\Http\PageInfo;
use Psr\Http\Message\ResponseInterface;

/**
 * 页面控制器
 * PageController
 */
#[Controller('admin/plugins/web-site/page')]
#[Middlewares([AuthenticateMiddleware::class, PermissionsMiddleware::class])]
class PageController extends AbstractController
{

    #[GetMapping("")]
    public function index(Render $render): ResponseInterface
    {
        return $render->render('index');
    }

    #[GetMapping("page-list")]
    public function pageList(PageInfo $pageInfo): ResponseInterface
    {
        return Response::json()->success(['data' => $pageInfo->getPageList()]);
    }

    #[GetMapping("code")]
    public function code(Render $render, PageInfo $pageInfo): ResponseInterface
    {
        return $render->render('code', [
            'code' => $pageInfo->getCode($this->request->query('page'))
        ]);
    }

    #[GetMapping("edit-page")]
    public function editPage(Render $render, PageInfo $pageInfo): ResponseInterface
    {
        return $render->render('edit-page', [
            'code' => $pageInfo->getCode($this->request->query('page'))
        ]);
    }

    #[PostMapping("code")]
    public function saveCode(PageInfo $pageInfo): ResponseInterface
    {
        $pageInfo->setCode($this->request->post('page'), $this->request->post('code'));
        return Response::json()->success();
    }


    #[PostMapping('save')]
    public function save(PageInfo $pageInfo): ResponseInterface
    {
        $pageInfo->setPage($this->request->post('page'), $this->request->post());

        return Response::json()->success();
    }
}